﻿#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif

#include "DQueryController.h"

#include "DQueryParaMgr.h"
#include "DSqliteMgr.h"

DQueryController::DQueryController() {}

std::vector<DUIHomeworkItem> DQueryController::query()
{
    DObjectPtr<DSqliteMgr> sqliteMgr;

    // student
    DObjectPtr<DQueryParaMgr> queryPara;
    const std::vector<int>& vecStatus = queryPara->getHomeworkStatus();
    const std::vector<int>& vecType = queryPara->getHomeworkTypeID();
    const std::vector<int>& vecStudentID = queryPara->getStudentID();

    const bool bYes =
        std::any_of(vecStatus.begin(), vecStatus.end(),
                    [](int s) { return s == (int)HomeworkStatus::YES; });
    const bool bNo =
        std::any_of(vecStatus.begin(), vecStatus.end(),
                    [](int s) { return s == (int)HomeworkStatus::No; });

    auto [start, end] = queryPara->getDate();

    std::vector<DUIHomeworkItem> vecItemRet;
    for (const auto& idStudent : vecStudentID)
    {
        DUIHomeworkItem uiItem;
        if (!sqliteMgr->getStudent(uiItem.student, idStudent))
        {
            continue;
        }

        uiItem.bYes = bYes;
        uiItem.bNo = bNo;
        for (auto idHomeworkType : vecType)
        {
            const std::vector<DUISubmit> vecTmp = sqliteMgr->getSubmitYesNoData(
                idStudent, start, end, idHomeworkType);

            uiItem.vecSubmitAll.insert(uiItem.vecSubmitAll.begin(),
                                       vecTmp.begin(), vecTmp.end());
        }

        vecItemRet.push_back(uiItem);
    }

    return vecItemRet;
}
